<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Blob和文件切片</title>
</head>

<body>
  <div>
    <span>文件对象</span>
    <input type="file" id="fileInput">
  </div>
  <div>
    blob协议
    <input type="file" id="blob_file">
    <img id="pic" width="300" />
  </div>
  <script>
    var file = null, files = null, fileChunkList = null
    fileInput.addEventListener('change', (e) => {
      files = e.target.files
      // console.log(e.target.files);
      file = e.target.files[0] //es6文件对象
      // console.log(file);
      // console.log(Object.prototype.toString.call(file));
      // console.log(file.slice(0, 102400));
      // console.log(Object.prototype.toString.call(file.slice(0, 102400)));

      let cur = 0, size = 1024 * 1024 // 1M
      // cur 开始， size 大小
      fileChunkList = []
      while (cur < file.size) {
        fileChunkList.push({
          file: file.slice(cur, cur + size)
        })
        cur += size
      }
      // console.log(fileChunkList);
    })
    blob_file.addEventListener('change', (e) => {
      const file = e.target.files[0]
      const URL = window.URL
      const objectUrl = URL.createObjectURL(file)
      console.log(objectUrl);

      pic.src = objectUrl
      pic.onload = () => {
        URL.revokeObjectURL(objectUrl);
        // 不用上传服务器就可以预览图片
        console.log('释放url完毕');
      }
    })
  </script>
</body>

</html>